package com.gyy.Thermometry;

import com.gyy.sdk.HCNetSDK;
import java.io.*;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @Author sdk支持
 * @Date 2021/3/16 17:54
 */
public class JpegWithAppendData {
    static HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;

    /**
     * 设备抓图附加全屏测温数据
     *
     * @param lUserID     用户登录句柄
     * @param iChannelNum 通道号
     */
    public void jpegWithAppendData(int lUserID, int iChannelNum) {
        boolean bRet;
        HCNetSDK.NET_DVR_JPEGPICTURE_WITH_APPENDDATA m_strJpegWithAppendData = new HCNetSDK.NET_DVR_JPEGPICTURE_WITH_APPENDDATA();
        m_strJpegWithAppendData.dwSize = m_strJpegWithAppendData.size();
        HCNetSDK.BYTE_ARRAY ptrJpegByte = new HCNetSDK.BYTE_ARRAY(8 * 1024 * 1024);
        HCNetSDK.BYTE_ARRAY ptrP2PDataByte = new HCNetSDK.BYTE_ARRAY(4 * 1024 * 1024);
        m_strJpegWithAppendData.pJpegPicBuff = ptrJpegByte.getPointer();
        m_strJpegWithAppendData.pP2PDataBuff = ptrP2PDataByte.getPointer();
        m_strJpegWithAppendData.dwJpegPicLen = 8 * 1024 * 1024;
        m_strJpegWithAppendData.dwP2PDataLen = 4 * 1024 * 1024;


        m_strJpegWithAppendData.dwJpegPicHeight = 200;
        m_strJpegWithAppendData.dwJpegPicWidth = 200;

        m_strJpegWithAppendData.write();
        bRet = hCNetSDK.NET_DVR_CaptureJPEGPicture_WithAppendData(lUserID, iChannelNum, m_strJpegWithAppendData);

        if (bRet == true) {
            //测温数据
            if (m_strJpegWithAppendData.dwP2PDataLen > 0) {
                SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
                String newName = sf.format(new Date());
                File dir = new File(".\\pic\\");
                if (!dir.exists()) {
                    dir.mkdir();
                }
                FileOutputStream fout;
                BufferedWriter fout1;
                try {
                    fout = new FileOutputStream(".\\pic\\" + "测温" + newName + ".data");
                    ByteBuffer bufferss = m_strJpegWithAppendData.pP2PDataBuff.getByteBuffer(0, m_strJpegWithAppendData.dwP2PDataLen);
                    byte[] bytess = new byte[m_strJpegWithAppendData.dwP2PDataLen];
                    bufferss.rewind();
                    bufferss.get(bytess);
                    fout.write(bytess);
                    fout.close();
                    fout1 = new BufferedWriter(new FileWriter(dir + "\\测温" + newName + ".csv"));
                    String tempdata;
                    byte[] byTempData = new byte[4];
                    for (int i = 1; i <= m_strJpegWithAppendData.dwJpegPicHeight; i++) {
                        for (int j = 1; j <= m_strJpegWithAppendData.dwJpegPicWidth; j++) {
                            ByteBuffer TempDatabuffers = m_strJpegWithAppendData.pP2PDataBuff.getByteBuffer((i - 1) * (j - 1) * 4, 4);
                            TempDatabuffers.get(byTempData);
                            int l;
                            l = byTempData[0];
                            l &= 0xff;
                            l |= ((long) byTempData[1] << 8);
                            l &= 0xffff;
                            l |= ((long) byTempData[2] << 16);
                            l &= 0xffffff;
                            l |= ((long) byTempData[3] << 24);
                            System.out.println("行数：" + i + "列数：" + j + "温度数据：" + Float.intBitsToFloat(l));
                            tempdata = " " + Float.intBitsToFloat(l);
                            fout1.write(tempdata);
                            fout1.write(",");
                        }
                        fout1.newLine();
                    }
                    fout1.close();
                } catch (FileNotFoundException e) {
                    System.out.println("路径不存在");
                } catch (IOException e) {
                    System.out.println("异常");
                }
            }
            //测温图片
            if (m_strJpegWithAppendData.dwJpegPicLen > 0) {
                SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
                String newName = sf.format(new Date());
                FileOutputStream pic;
                try {
                    pic = new FileOutputStream(".\\pic\\" + "测温_" + newName + ".jpg");
                    ByteBuffer buffers = m_strJpegWithAppendData.pJpegPicBuff.getByteBuffer(0, m_strJpegWithAppendData.dwJpegPicLen);
                    byte[] bytes = new byte[m_strJpegWithAppendData.dwJpegPicLen];
                    buffers.rewind();
                    buffers.get(bytes);
                    pic.write(bytes);
                    pic.close();

                } catch (FileNotFoundException e) {
                    System.out.println("路径不存在");
                } catch (IOException e) {
                    System.out.println("异常");
                }
            }
        } else {
            System.out.println("抓图叠加温度信息失败，错误码" + hCNetSDK.NET_DVR_GetLastError());
        }
    }

    /**
     * 设备抓图
     *
     * @param lUserID     用户登录句柄
     * @param iChannelNum 通道号
     */
    public void getCameraJpeg(int lUserID, int iChannelNum) {
        boolean bRet;
        HCNetSDK.NET_DVR_JPEGPICTURE_WITH_APPENDDATA m_strJpegWithAppendData = new HCNetSDK.NET_DVR_JPEGPICTURE_WITH_APPENDDATA();
        m_strJpegWithAppendData.dwSize = m_strJpegWithAppendData.size();
        HCNetSDK.BYTE_ARRAY ptrJpegByte = new HCNetSDK.BYTE_ARRAY(8 * 1024 * 1024);
        HCNetSDK.BYTE_ARRAY ptrP2PDataByte = new HCNetSDK.BYTE_ARRAY(4 * 1024 * 1024);
        m_strJpegWithAppendData.pJpegPicBuff = ptrJpegByte.getPointer();
        m_strJpegWithAppendData.pP2PDataBuff = ptrP2PDataByte.getPointer();
        m_strJpegWithAppendData.dwJpegPicLen = 8 * 1024 * 1024;
        m_strJpegWithAppendData.dwP2PDataLen = 4 * 1024 * 1024;

        m_strJpegWithAppendData.write();
        bRet = hCNetSDK.NET_DVR_CaptureJPEGPicture_WithAppendData(lUserID, iChannelNum, m_strJpegWithAppendData);

        if (bRet == true) {
            //测温图片
            if (m_strJpegWithAppendData.dwJpegPicLen > 0) {
                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd_HHmmssSSS");
                String newName = sf.format(new Date());
                FileOutputStream pic;
                try {
                    pic = new FileOutputStream(".\\pic\\" + "cutPicture_" + newName + ".jpg");
                    ByteBuffer buffers = m_strJpegWithAppendData.pJpegPicBuff.getByteBuffer(0, m_strJpegWithAppendData.dwJpegPicLen);
                    byte[] bytes = new byte[m_strJpegWithAppendData.dwJpegPicLen];
                    buffers.rewind();
                    buffers.get(bytes);
                    pic.write(bytes);
                    pic.close();

                } catch (FileNotFoundException e) {
                    System.out.println("路径不存在");
                } catch (IOException e) {
                    System.out.println("异常");
                }
            }
        } else {
            System.out.println("抓图失败，错误码" + hCNetSDK.NET_DVR_GetLastError());
        }
    }

}
